var app = getApp(),
  $ = require("../../utils/httpHelper.js"),
  WxNotificationCenter = require("../../WxNotificationCenter/WxNotificationCenter.js")//通知传参
var wxParse = require("../../wxParse/wxParse.js")
Page({
  data: {
    objarr: [],
    height: '',
    width: '',
    good_info:{},
    currentTab: 0,
    goodsinfo:{},
    isfav:true,
    goods_id:0,
    status:'',
    yhq_status:true,
    goods_gg_status: true,
    time_unit:'',
    num:1,
    speclist:[],
    speclist_array: [],
    coupon_list:[],
    activeid:'',
    sell_p:'',
    sku_id:'',
    add_cart:false,
    comment_list:[]
  },
  onShareAppMessage: function (res) {
    app.shareApp(res)
  },
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight,
          width: res.windowWidth
        })
      }
    });
    
    if (!app.check_empty(options.id)){
      var rd_session = wx.getStorageSync('rd_session');
      this.setData({
        goods_id:options.id,
      })
      $.httpPost("good_detail", { good_id: options.id,rd_session:rd_session}, (data, err) => {
        this.setData({
          good_info:data.data.good_info,
          isfav: data.data.good_info.is_favorite,
          comment_list: data.data.comment_list
        });
        wxParse.wxParse("about", "html", data.data.good_info.content, this)
      })
    }
    
  },

  clicktab(e) {
    console.log(e.target.dataset.tab)
    let tab = e.target.dataset.tab
    if (this.data.currentTab != tab) {
      this.setData({
        currentTab: tab
      })
    }
  },
  favorite(){
    if(this.data.isfav){
      var rd_session = wx.getStorageSync('rd_session');
      $.httpPost("delete_favorite_good", { good_id: this.data.goods_id, rd_session: rd_session }, (data, err) => {
        this.setData({
          isfav: data.favorite
        })
      })
    }else{
      var rd_session = wx.getStorageSync('rd_session');
      $.httpPost("add_favorite_good", { good_id: this.data.goods_id, rd_session: rd_session,types:1}, (data, err) => {
        this.setData({
          isfav: data.favorite
        })
      })
    }
    
  },
  yhqShow(){
    $.httpPost("coupon_list", {}, (data, err) => {
      this.setData({
        yhq_status: false,
        coupon_list:data.data.list
      })
    })
    
  },
  getYhq(e){
    console.log(e)
    var rd_session = wx.getStorageSync('rd_session');
    $.httpPost("get_coupon", { coupon_id: e.currentTarget.dataset.id, rd_session: rd_session}, (data, err) => {
      wx.showToast({
        title: data.result,
      })
      this.yhqHide()
    })
  },
  yhqHide(){
    this.setData({
      yhq_status: true
    })
  },
  canlGG(){
    this.setData({
      goods_gg_status: true
    })
  },
  goodsGG() {
    this.setData({
      goods_gg_status: false,
    })
  },
  addCart(){
    this.setData({
      goods_gg_status: false,
      add_cart:true
    })
  },
  //待付款页面
  obligation(){
      var rd_session = wx.getStorageSync('rd_session');
      if(this.data.add_cart){
        $.httpPost("add_cart", { good_id: this.data.goods_id, total_num: this.data.num, rd_session: rd_session},(data,err) => {
          wx.showToast({
            title: data.message,
            duration: 2000,
          })
          this.setData({
            add_cart: false
          })
        })
      }else{
        wx.navigateTo({
          url: '/pages/obligation/obligation?&num=' + this.data.num
          + "&good_id=" + this.data.goods_id
        })
      }
      this.setData({
        goods_gg_status: true,
      })
  },

  sub(){
    console.log(this.data.num)
    if(this.data.num == 1) return;
    this.setData({
      num:this.data.num-1
    })
  },
  add(){
    this.setData({
      num: this.data.num+1
    })
  },
  seleAct(e){

    var active_id = e.target.dataset.id
    var s = this.data.spec_list;
    var pk = parseInt(e.target.dataset.pkey);
    var ck = parseInt(e.target.dataset.key);
    this.data.speclist_array[pk] = active_id;
    s.forEach(function (value,index) {
      value.value_list.forEach(function (valuea, indexa) {
        if (index == pk) { s[index]['value_list'][indexa]['active'] = false;}
      });

    });
    s[pk].value_list[ck]['active']=true;
    this.setData({
      activeid: active_id,
      spec_list:s
    })
    
    var selstr = this.data.speclist_array.join(',');
    console.log(this.data.speclist_array)
    if (this.data.speclist_array.length == this.data.spec_list.length){
      $.httpPost("get_sku", { spec_ids: selstr, good_id: this.data.goods_id,order_type:this.data.status }, (data, err) => {
        this.setData({
          sell_p: data.data.price,
          sku_id: data.data.id
        })
      })
    }
    

  }

  
});